package controllers

import (
	"CRMSystem/models"
	"encoding/json"
)

type CustomerController struct {
	MainController
}

// CustomerList 客户列表
func (c *CustomerController) CustomerList() {
	c.IfHasSession()
	var from int64
	var to int64
	currentPage, _ := c.GetInt64("currentPage")
	pageSize, _ := c.GetInt64("pageSize")
	from = (currentPage - 1) * pageSize
	to = currentPage * pageSize
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetCustomerListInformationByContactID(contact.ContactId, from, to)

	totalPage := i/pageSize + 1

	if i > 0 && b {
		c.Data["json"] = Status{Date: list, TotalPage: totalPage, ErrCode: GET_OK, Msg: "CustomerList:success"}
		c.ServeJSON()
		c.StopRun()
	} else if currentPage > totalPage {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// SearchCustomerList 搜索客户列表
func (c *CustomerController) SearchCustomerList() {
	c.IfHasSession()
	search := c.GetString("search")
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetCustomerListInformationByContactIDCustomer(search, contact.ContactId)
	if i > 0 && b {
		c.Data["json"] = Status{Date: list, ErrCode: GET_OK, Msg: "SearchCustomerList:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// CustomerLostList 流失客户列表
func (c *CustomerController) CustomerLostList() {
	c.IfHasSession()
	var from int64
	var to int64
	currentPage, _ := c.GetInt64("currentPage")
	pageSize, _ := c.GetInt64("pageSize")
	from = (currentPage - 1) * pageSize
	to = currentPage * pageSize
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetLostCustomerListInformationByContactID(contact.ContactId, from, to)
	totalPage := i/pageSize + 1
	if i > 0 && b {
		c.Data["json"] = Status{Date: list, TotalPage: totalPage, ErrCode: POST_OK, Msg: "CustomerLostList:success"}
		c.ServeJSON()
		c.StopRun()
	} else if currentPage > totalPage {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
		c.Redirect("/page/err400", 302)
		c.ServeJSON()
	}

}

// SearchCustomerLostList 搜索流失客户列表
func (c *CustomerController) SearchCustomerLostList() {
	c.IfHasSession()
	search := c.GetString("search")
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetLostCustomerListInformationByContactIDCustomer(search, contact.ContactId)
	if i > 0 && b {
		c.Data["json"] = Status{Date: list, ErrCode: GET_OK, Msg: "SearchCustomerLostList:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// CustomerPlanList 可开展服务客户列表
func (c *CustomerController) CustomerPlanList() {
	c.IfHasSession()
	var from int64
	var to int64
	currentPage, _ := c.GetInt64("currentPage")
	pageSize, _ := c.GetInt64("pageSize")
	from = (currentPage - 1) * pageSize
	to = currentPage * pageSize
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetPlanCustomerListInformationByContactID(contact.ContactId, from, to)
	totalPage := i/pageSize + 1
	if i > 0 && b {
		c.Data["json"] = Status{Date: list, TotalPage: totalPage, ErrCode: POST_OK, Msg: "CustomerLostList:success"}
		c.ServeJSON()
		c.StopRun()
	} else if currentPage > totalPage {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
		c.Redirect("/page/err400", 302)
		c.ServeJSON()
	}

}

// SearchCustomerPlanList 搜索可开展服务客户列表
func (c *CustomerController) SearchCustomerPlanList() {
	c.IfHasSession()
	search := c.GetString("search")
	contact, b := GetSessionContact(c.GetSession("Contact"))
	list, i := models.GetPlanCustomerListInformationByContactIDCustomer(search, contact.ContactId)
	if i > 0 && b {
		c.Data["json"] = Status{Date: list, ErrCode: GET_OK, Msg: "SearchCustomerPlanList:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// ViewCustomer 展示客户重定向
func (c *CustomerController) ViewCustomer() {
	customerID := c.GetString("customerID")
	redirectURL := "/page/service?customerID=" + customerID
	c.Data["json"] = map[string]string{"redirectUrl": redirectURL}
	c.ServeJSON()
}

// SubmitCustomer 提交客户
func (c *CustomerController) SubmitCustomer() {
	var submit models.CustomerSubmitModel
	contact, _ := GetSessionContact(c.GetSession("Contact"))
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &submit); err == nil {
		err = models.AddCustomer(submit, contact.ContactId)
		if err != nil {
			c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
			c.Redirect("/page/err400", 302)
			c.ServeJSON()
		}
		c.Data["json"] = Status{ErrCode: POST_OK, Msg: "SubmitCustomer success"}
		c.ServeJSON()
		c.StopRun()
	}

}
